package com.credithc.customers.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.customers.manager.ro.CustomerProductInfoRo;
import com.credithc.customers.manager.service.CustomerProductService;
import com.credithc.customers.manager.vo.CustomerProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhukai
 * @date 2019/1/22 16:32
 */
@Controller
@RequestMapping("/manager/yzt/v1/customer/product")
public class CustomerProductController {

    @Autowired
    private MessageFactory messageFactory;

    @Autowired
    private CustomerProductService customerProductService;


    @GetMapping("/list")
    @ResponseBody
    public Message getList(CustomerProductInfoRo ro, @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize",defaultValue = "20") Integer pageSize) {
        PageInfo<CustomerProductVo> list = customerProductService.getList(ro, pageNo, pageSize);
        Map<String, Object> pageInfo = new HashMap<>();
        pageInfo.put("pageInfo", list);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, pageInfo);
    }


    @GetMapping("/stats")
    @ResponseBody
    public Message stats(@RequestParam(name = "customerNo") String customerNo,@RequestParam(name = "statsType") String statsType) {
        String s = customerProductService.getProduct(customerNo, statsType);
        Map<String,String> map = new HashMap<>();
        map.put("pie",s);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    @GetMapping("/listDetail")
    @ResponseBody
    public Message getListDetail(@PathParam("customerNo") String customerNo) {
        List<CustomerProductVo> list = customerProductService.getListDetail(customerNo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, list);
    }

    @GetMapping("/list/{custmerNo}")
    @ResponseBody
    public Message listByPreCustomerNo(@PathVariable(name = "custmerNo", required = true) String custmerNo,
                                       @RequestParam(name = "pageNo", defaultValue = "0") @Min(message = "pageNo 最小值为0", value = Integer.MIN_VALUE) @Max(message = "pageNo 不能超过Integer类型最大值", value = Integer.MAX_VALUE) Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "0") @Min(message = "pageNo 最小值为0", value = Integer.MIN_VALUE) @Max(message = "pageSize 不能超过Integer类型最大值", value = Integer.MAX_VALUE) Integer pageSize) {
        PageInfo<CustomerProductVo> voPageInfo = customerProductService.listByPreCustomerNo(custmerNo, pageNo, pageSize);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, voPageInfo);
    }

}
